package p0;

public class Leetcode_2 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode r = new ListNode((l1.val + l2.val) % 10);
        int c = (l1.val + l2.val) / 10;
        l1 = l1.next;
        l2 = l2.next;
        // int c = 0;
        ListNode T = r;
        while (l1 != null || l2 != null || c > 0) {
            int ll1 = l1 == null ? 0 : l1.val;
            int ll2 = l2 == null ? 0 : l2.val;
            int t = ll1 + ll2 + c;
            c = t / 10;
            T.next = new ListNode(t % 10);
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
            T = T.next;
        }
        return r;
    }

    public static void main(String[] args) {

    }
}
